// pages/mes/bind-phone/bind-phone.js
const app = getApp();
import Toast from '../../../component/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        position: 1,
        phoneNumber: '',
        phoneCode: '',
        codeStatu: '获取验证码',
        isCountDown: false
    },
    pageItemClick(e) {
        let index = e.target.id;
        this.setData({
            position: index
        });
    },
    getPhoneNumber(e) {
        let that = this;
        if (!app.globalData.isLogin) {
            Toast("请先登录");
            return;
        }
        if (app.globalData.isBindPhone) {
            Toast("手机号已绑定");
            setTimeout(function() {
                console.log("绑定绑定")
                wx.navigateBack({
                    delta: 5
                });
            }, 1000);
            return;
        }
        console.log("UserInfoId:" + app.userInfo.Id + ",EncryptedData：" + e.detail.encryptedData + ",Iv：" + e.detail.iv);
        // return;
        if (e.detail.errMsg == 'getPhoneNumber:ok') {
            let data = {
                UserInfoId: app.userInfo.Id,
                EncryptedData: e.detail.encryptedData,
                Iv: e.detail.iv,
                BindType: 1
            };
            that.bindPhone(data);
        } else {
            Toast("请重新绑定！")
        }
    },
    countDown() {
        let countDown = 60;
        let timer;
        let that = this;
        timer = setInterval(function() {
            if(countDown == 0){
                that.setData({
                    codeStatu: '获取验证码',
                    isCountDown: false
                });
                clearTimeout(timer);
            }else{
                --countDown;
                that.setData({
                    codeStatu: countDown+'s',
                    isCountDown: true
                });
            }
        }, 1000);
    },
    bindSmsCodeClick() {
        // console.log("进入");
        if (!this.isPoneAvailable(this.data.phoneNumber)) {
            Toast("请填写正确的手机号码!");
            return;
        }
        // if (app.globalData.isBindPhone) {
        //     Toast("已存在绑定的手机号!");
        //     return;
        // }
        if (this.data.isCountDown) {
            return;
        }
        this.sendRegisterSMS(this.data.phoneNumber);
    },
    bindPhoneClick() {
        if (!this.isPoneAvailable(this.data.phoneNumber)) {
            Toast("请填写正确的手机号码!");
            return;
        }
        if (!this.data.phoneCode || this.data.phoneCode.length < 6) {
            Toast("请填写6位数验证码！");
            return;
        }
        let data = {
            UserInfoId: app.userInfo.Id,
            BindType: 2,
            MobileNumber: this.data.phoneNumber,
            Code: this.data.phoneCode
        };
        console.log("url:" + app.globalData.serverAddress.bindPhone + "," + JSON.stringify(data));
        this.bindPhone(data);
    },
    phoneNumber(e) {
        this.setData({
            phoneNumber: e.detail.value
        });
    },
    phoneCode(e) {
        this.setData({
            phoneCode: e.detail.value
        });
    },
    isPoneAvailable: function(pone) {
        var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if (!myreg.test(pone)) {
            return false;
        } else {
            return true;
        }
    },
    toAgreement() {
        app.globalData.globalWebUrl = app.globalData.agreementUrl.userAgreementUrl;
        wx.navigateTo({
            url: '/pages/agreement-web/agreement-web?title=闲将用户协议及隐私政策'
        })
    },
    /**
     * 网络请求
     */
    sendRegisterSMS(phoneNum) {
        let that = this;
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.sendRegisterSms,
            method: 'POST',
            data: {
                MobileNumber: phoneNum
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            success: function(res) {
                console.log(res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        that.countDown();
                    }
                } else {
                    console.error("getPhoneNumber:" + JSON.stringify(res));
                }
                Toast(res.data.Message);
            },
            fail: function(res) {
                console.log("getPhoneNumber:" + JSON.stringify(res));
            },
            complete:function(res){wx.hideLoading();}
        })
    },
    bindPhone(data) {
        wx.showLoading({
            title: '加载中...',
            mask:true
        })
        wx.request({
            url: app.globalData.serverAddress.bindPhone,
            method: 'POST',
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            success: function(res) {
                console.log(res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        app.globalData.isBindPhone = true;
                        app.userInfo = res.data.BodyContent;
                        wx.navigateBack({
                            delta: 1
                        });
                    }
                } else {
                    console.error("getPhoneNumber:" + JSON.stringify(res));
                }
                Toast(res.data.Message);
            },
            fail: function(res) {
                console.log("getPhoneNumber:" + JSON.stringify(res));
            },
            complete: function (res) { wx.hideLoading(); }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})